package com.ycsj.goldmedalnewconcept.student.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.design.widget.Snackbar;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.base.SpKeys;
import com.ycsj.common.manager.CustomDialogManager;
import com.ycsj.common.utils.ExitLogonUtil;
import com.ycsj.common.utils.MaxTextLengthFilter;
import com.ycsj.common.utils.SPUtil;
import com.ycsj.common.utils.SnackbarUtil;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.ResultInfo;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.common.util.StateCodeUtils;
import com.ycsj.goldmedalnewconcept.main.activity.LoginActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 学生端--宠物领取第四页
 * Created by Kevin on 2019/2/28
 */

public class StudentGetpetFourActivity extends BaseActivity {

    @BindView(R.id.ll_main)
    LinearLayout llMain;

    @BindView(R.id.iv_btn)
    ImageView iv_btn;
    @BindView(R.id.iv_pet)
    ImageView iv_pet;

    @BindView(R.id.et_name)
    EditText et_name;

    @BindView(R.id.iv_flower1)
    ImageView iv_flower1;
    @BindView(R.id.iv_flower2)
    ImageView iv_flower2;
    @BindView(R.id.iv_flower3)
    ImageView iv_flower3;

    //控件做动画的集合
    private AnimatorSet animatorSet;

    //上传给服务器的宠物的对应编号id 0狗 1怪物 2老虎 3大象
    private int petId = 0;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_student_getpet_four;
    }

    @Override
    protected void init() {

        initStateBar();

        //初始化宠物形象
        initPet();

        //初始化输入框
        initEditText();

        //初始化动画
        initAnim();
    }

    /**
     * 初始化输入框
     */
    private void initEditText() {
        //按钮初次进入不可点击
        iv_btn.setEnabled(false);
        et_name.setFilters(new InputFilter[]{new MaxTextLengthFilter(21)});

        et_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                synvLoginState();
            }
        });
    }

    /**
     * 初始化动画
     */
    private void initAnim() {

        ObjectAnimator xOneAnim = ObjectAnimator.ofFloat(iv_flower1, "alpha", 1, 0, 1);
        ObjectAnimator xTwoAnim = ObjectAnimator.ofFloat(iv_flower2, "alpha", 1, 0, 1);
        ObjectAnimator xTheeAnim = ObjectAnimator.ofFloat(iv_flower3, "alpha", 1, 0, 1);

        xOneAnim.setRepeatCount(ValueAnimator.INFINITE);
        xOneAnim.setRepeatMode(ValueAnimator.RESTART);
        xOneAnim.setDuration(4000);

        xTwoAnim.setRepeatCount(ValueAnimator.INFINITE);
        xTwoAnim.setRepeatMode(ValueAnimator.RESTART);
        xTwoAnim.setDuration(4500);

        xTheeAnim.setRepeatCount(ValueAnimator.INFINITE);
        xTheeAnim.setRepeatMode(ValueAnimator.RESTART);
        xTheeAnim.setDuration(5000);

        animatorSet = new AnimatorSet();
        animatorSet.playTogether(xOneAnim, xTwoAnim, xTheeAnim);
        animatorSet.setInterpolator(new LinearInterpolator());
        animatorSet.start();
    }

    /**
     * 同步登录按钮的方法
     */
    private void synvLoginState() {
        String name = et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            iv_btn.setImageResource(R.drawable.student_choosepet_unclick);
            iv_btn.setEnabled(false);
        } else {
            iv_btn.setImageResource(R.drawable.student_choosepet_click);
            iv_btn.setEnabled(true);
        }
    }

    /**
     * 初始化宠物形象
     */
    private void initPet() {

        String type = SPUtil.getString(this, SpKeys.PETTYPE);

        switch (type) {
            case "1":
                //老虎
                iv_pet.setImageResource(R.drawable.student_choosepet_tiger);
                petId = 2;
                break;
            case "2":
                //大象
                iv_pet.setImageResource(R.drawable.student_choosepet_elephant);
                petId = 3;
                break;
            case "3":
                //狗
                iv_pet.setImageResource(R.drawable.student_choosepet_dog);
                petId = 0;
                break;
            case "4":
                //怪物
                iv_pet.setImageResource(R.drawable.student_choosepet_monster);
                petId = 1;
                break;
        }
    }

    /**
     * 页面控件的点击事件
     *
     * @param v
     */
    @OnClick({R.id.iv_btn})
    public void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.iv_btn:

                iv_btn.setClickable(false);
                //保存宠物信息给服务器
                doSavePetInfo();
                break;
        }
    }

    /**
     * 保存宠物信息
     */
    private void doSavePetInfo() {
        String petName = et_name.getText().toString().trim();
        if (TextUtils.isEmpty(petName)) {
            ToastUtils.showShort("主人，请先为我取个好听的昵称吧");
            iv_btn.setClickable(true);
            return;
        }

        CustomDialogManager.show(this, "上传中...");
        Map<String, String> map = new HashMap<>();
        map.put("type", "1");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("apptoken", AccountUtils.getToken(this));
        map.put("platform", "2");
        map.put("pettype", petId + "");
        map.put("petname", petName);

        HttpUtils.post(HttpAddress.SAVEPETMSG, map, this, new HttpCallBack<ResultInfo>() {
            @Override
            public void onSuccess(ResultInfo resultInfo) {
                CustomDialogManager.dissmiss();
                if ("200".equals(resultInfo.getState())) {
                    //保存成功
                    BusInfo info = new BusInfo();
                    info.type = Constants.CLOSEPETPAGE;
                    EventBus.getDefault().post(info);

                    //上传成功后保存本地的宠物信息
                    SPUtil.put(StudentGetpetFourActivity.this, SpKeys.SERVICEPETTYPE, petId + "");

                    //跳转到学生端主界面
                    UiUtils.startActivity(StudentGetpetFourActivity.this, StudentMainActivity.class);

                    //关闭当前页面
                    finish();
                } else if ("201003".equals(resultInfo.getState())) {
                    //已经领取过宠物,重新跳转到登录界面
                    SnackbarUtil.ShortSnackbar(llMain, "已经领取过宠物了,请重新登录", Snackbar.LENGTH_SHORT).show();

                    //退出登录
                    ExitLogonUtil.exitLogin(StudentGetpetFourActivity.this);

                    //保存用户角色
                    SPUtil.put(StudentGetpetFourActivity.this, SpKeys.ROLETYPE, "0");

                    //重新跳转到登录界面
                    UiUtils.startActivity(StudentGetpetFourActivity.this, LoginActivity.class);

                    finish();
                } else if ("101107".equals(resultInfo.getState()) ||
                        "101108".equals(resultInfo.getState())) {

                    //重新跳转到登录界面
                    SnackbarUtil.ShortSnackbar(llMain, "登录已失效，请重新登录", Snackbar.LENGTH_SHORT).show();

                    //退出登录
                    ExitLogonUtil.exitLogin(StudentGetpetFourActivity.this);

                    //保存用户角色
                    SPUtil.put(StudentGetpetFourActivity.this, SpKeys.ROLETYPE, "0");

                    //重新跳转到登录界面
                    UiUtils.startActivity(StudentGetpetFourActivity.this, LoginActivity.class);

                    finish();
                } else {
                    StateCodeUtils.showSateCode(resultInfo.getState());
                }

                iv_btn.setClickable(true);
            }

            @Override
            public void onError(String e) {
                CustomDialogManager.dissmiss();
                ToastUtils.showShort(BaseApplication.ERROR);
                iv_btn.setClickable(true);
            }
        });
    }
}
